layui.use(['layer','form','table','element'],function () {
    var layer = layui.layer
        ,form = layui.form
        ,table =layui.table
        ,element = layui.element;


    //初始化数据表格
     table.render({
         elem:"#data"
         ,height: 340
         ,url:"/admin/getBusData"
         ,cellMinWidth: 80
         ,cols: [[
              {field:'id',width:40,title: 'ID', sort: true}
             ,{field:'busName', title: '车型',width: 100}
             ,{field:'idCard', width: 110   , title: '车牌号'}
             // ,{field:'rentMoney', width: 100   , title: '每公里租金'}
             // ,{field:'distanceMonth', width: 140   , title: '本月行驶公里数'}
             ,{field:'user.userName',width: 180   ,  title: '租车司机',templet:"#payTem"}
             ,{field:'showCreateTime',width:110,title: '创建时间',sort: true}
             ,{field:'',width:200,title: '操作',toolbar: '#barDemo'}
         ]]
         ,page:true
     });


     //监听工具条
     table.on('tool(dataTable)',function (obj) {
         var data = obj.data;
         if(obj.event == 'detail'){
             //查看，修改
             parent.layui.element.tabAdd('tabs',{
                 title:"查看和修改",
                 content:"<iframe  src='/admin/toEdit?id="+data.id+"'  frameborder='0'  style='width:100%;height: 100%'></iframe>",
                 id:"toBusInfoView"
             });
             rightMenu();
             iframeWH();
             parent.layui.element.tabChange('tabs','toBusInfoView');

         }else if(obj.event == 'del'){
             layer.msg('删除中...', { icon: 16, shade: [0.5, '#f5f5f5'], scrollbar: false, offset: '50%', time: 300000 });

             //删除
             $.ajax({
                 type:"POST"
                 ,data:{
                     id:data.id
                 }
                 ,url:"/admin/deleteBus"
                 ,success:function (res) {
                     layer.msg(res.message);
                 }
                 ,error:function () {
                     layer.msg("系统错误！");
                 }
             });
         }else  if(obj.event == "position"){
             //车辆定位
             parent.layui.element.tabAdd('tabs',{
                 title:"车辆定位",
                 content:"<iframe  src='/admin/position?id="+data.id+"'  frameborder='0'  style='width:100%;height: 100%'></iframe>",
                 id:"toPosition"
             });
             rightMenu();
             iframeWH();
             parent.layui.element.tabChange('tabs','toPosition');
         }else  if(obj.event == "open"){

         }else  if(obj.event == "close"){

         }
     });


    $("#payBtn").bind("click",function () {
        alert("hello");
    })

    function rightMenu(){

        $(".layui-tab-title li",window.parent.document).on("contextmenu",function (e) {
            var rightmenu = $(".rightmenu",window.parent.document);
            rightmenu.find("li").attr('data-id',$(this).attr("lay-id"));
            l = e.clientX;
            t = e.clientY;
            rightmenu.css({ left:l, top:t}).show();
            return false;
        });
        //左键点击隐藏
        $("body,.layui-tab-title li",window.parent.document).click(function(){
            $(".rightmenu").hide();
        });

    }

    function iframeWH(){
        var H = $(window).height();
        $("iframe",window.parent.document).css("height",H+"px");
    }
     
     //搜索
    $("#search").on("click",function () {
        table.reload("data",{
            page:{
                curr:1
            }
            ,where:{
                busName:$("#busName").val(),
                idCard:$("#idCard").val()
            }
        })
    });

    $("#add").on("click",function () {

        parent.layui.element.tabAdd('tabs',{
            title:"新增出租车",
            content:"<iframe  src='/admin/rentToAdd'  frameborder='0'  style='width:100%;height: 100%'></iframe>",
            id:"bus_add"
        });
        rightMenu();
        iframeWH();
        parent.layui.element.tabChange('tabs','bus_add');


    })

    //搜索
    $("#open").on("click",function () {
        layer.msg("开启成功！");


        //开启模拟定位
        $.ajax({
            type:"POST"
            ,url:"/admin/openPosition"
            ,success:function (res) {
            }
            ,error:function () {
                layer.msg("系统错误！");
            }
        });


    });


    //搜索
    $("#close").on("click",function () {

        layer.msg('关闭中...', { icon: 16, shade: [0.5, '#f5f5f5'], scrollbar: false, offset: '50%', time: 300000 });


        //关闭模拟定位
        $.ajax({
            type:"POST"
            ,url:"/admin/closePosition"
            ,success:function (res) {
                layer.msg(res.message);
            }
            ,error:function () {
                layer.msg("系统错误！");
            }
        });

    });


    //搜索
    $("#sendRent").on("click",function () {

        layer.msg('发送中...', { icon: 16, shade: [0.5, '#f5f5f5'], scrollbar: false, offset: '50%', time: 300000 });

        //发送账单
        $.ajax({
            type:"POST"
            ,url:"/admin/sendRent"
            ,success:function (res) {
                layer.msg(res.message);
            }
            ,error:function () {
                layer.msg("系统错误！");
            }
        });

    });
});
